package com.itheima.service.impl;


import cn.hutool.core.util.IdUtil;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.mapper.CategoryMapper;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.vo.CategoryVo;
import com.itheima.vo.DishVo;
import com.itheima.vo.PagingQueryVo;
import com.itheima.vo.UserCategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :WuJunJie
 * packageName :com.itheima.service.impl
 * fileName :CategoryImpl
 * creationTime :2022-11-22 14:57
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishService dishService;


    @Override
    public Boolean insertCategory(Category category) {
        String  snowflake = IdUtil.getSnowflake().nextIdStr();
        category.setId(snowflake);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(ThreadLocalUtil.getid());
        category.setUpdateUser(ThreadLocalUtil.getid());
        Integer insert=categoryMapper.insertCategory(category);
        return insert>0;
    }

    @Override
    public Boolean updateCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(ThreadLocalUtil.getid());
        Integer update=categoryMapper.updateCategory(category);
        return update>0;
    }

    @Override
    public Boolean deleteCategory(String id) {
        List<DishVo> dishVos = dishService.selectByCategoryId(id);
        if (!dishVos.isEmpty()){
            return false;
        }
        Integer delete=categoryMapper.deleteCategory(id);
        return delete>0;
    }

    @Override
    public List<Category> selectByType(String type) {
        List<Category> categories=categoryMapper.selectByType(type);
        return categories;
    }

    @Override
    public PagingQueryVo<Category> selectPage(Integer page, Integer pageSize) {
        Integer start=(page-1)*pageSize;
        List<Category> categories = categoryMapper.selectPage(start, pageSize);
        PagingQueryVo<Category> pagingQueryVo = new PagingQueryVo<>();
        pagingQueryVo.setRecords(categories);
        pagingQueryVo.setSize(pageSize);
        pagingQueryVo.setCurrent(page);
        pagingQueryVo.setTotal(categoryMapper.countTotal());
        return pagingQueryVo;
    }

    @Override
    public List<UserCategoryVo> selectCategories() {
        List<Category> categories=categoryMapper.selectCategories();
        List<UserCategoryVo> categoryVos=new ArrayList<>();
        for (Category category : categories) {
            UserCategoryVo  categoryVo = new UserCategoryVo();
            BeanUtils.copyProperties(category,categoryVo);
            categoryVos.add(categoryVo);
        }
        return categoryVos;
    }
}
